$(function () {
    page(1,10);
    /**
     * 用于选中全部
     */
    $("#checkboxs").click(function () {
        var boxs = $(":input[name='checkbox']")
        for (let i = 0; i < boxs.length; i++) {
            boxs[i].checked=this.checked
        }
    })

    /**
     * 用于删除选中
     */
    $("#delete_submit").click(function (data) {
        layer.confirm("您确定要删除?",  {icon: 3, title:'提示'}, function(e) {
            $.post("notice/deleteByNids", $("#tabForm").serialize(), function (data) {
                page(1,10);
                window.scrollTo(0, 50);
            })
            layer.close(e)
        });
    })

})

function page(current,pageSize) {
    $.post("notice/findByPage",{currentPage:current,pageSize:pageSize},function (json) {
        if (json != "index.jsp" ){
            if (json.flag){
                var notices = json.data
                $("#totalPage").html(notices.totalPage)
                $("#totalCount").html(notices.totalCount)
                var notice_lis = ''
                for (var i = 0; i < notices.list.length; i++) {
                    var notice = notices.list[i]
                    var li = ""
                    li += '<tr>\n' +
                        '        <td><input name="checkbox" value="'+notice.nid+'" type="checkbox"></td>\n' +
                        '        <td>'+i+'</td>\n' +
                        '        <td>'+notice.theme+'</td>\n' +
                        '        <td>'+notice.content+'&nbsp;</td>\n' +
                        '        <td>'+notice.time+'</td>\n' +
                        '        <td>\n' +
                        '            <button onclick="updateUser('+notice.nid+','+notices.currentPage+')" type="button" class="btn btn-info">修改</button>\n' +
                        '            <button onclick="deleteUser('+notice.nid+','+notices.currentPage+')" type="button" class="btn btn-danger">删除</button>\n' +
                        '        </td>\n' +
                        '    </tr>'
                    notice_lis += li
                }
                $("#RTable").html(notice_lis)
            }

            var beforeNum = notices.currentPage-1;
            if (beforeNum <= 0){
                beforeNum = 1;
            }
            var lis = '<li onclick="javascript:page('+1+','+pageSize+')"><a href="javascript:void(0)">首页</a></li>\n' +
                '<li onclick="javascript:page('+beforeNum+','+pageSize+')"> <a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">&laquo;</span> </a></li>'
            //开始页码
            let begin;
            //结束页码
            let end;
            if (notices.totalPage <= 10){
                begin = 1;
                end = notices.totalPage;
            }else {
                begin = (notices.currentPage - 5);
                end = (notices.currentPage + 4);
                if (begin < 1){
                    begin = 1;
                    end = begin + 9;
                }
                if (end > notices.totalPage){
                    end = notices.totalPage;
                    begin = end - 9;
                }
            }
            for (let i = begin; i <= end; i++) {
                var l = ""
                if(notices.currentPage  == i){
                    l = '<li class="active" onclick="javascript:page('+i+','+pageSize+')"><a href="javascript:void(0);">'+i+'</a></li>'
                }else {
                    l = '<li onclick="javascript:page('+i+','+pageSize+')"><a href="javascript:void(0);">'+i+'</a></li>'
                }
                lis += l
            }

            var nextNum = notices.currentPage +1;
            if (nextNum >= notices.totalPage){
                nextNum = notices.totalPage;
            }
            lis += '<li onclick="javascript:page('+nextNum+','+pageSize+')"><a href="javascript:void(0);" aria-label="Next"><span aria-hidden="true">&raquo;</span></a> </li>\n' +
                '<li onclick="javascript:page('+notices.totalPage+','+pageSize+')"><a href="javascript:void(0);">尾页</a></li>'

            $("#paging").html(lis)
        }else {
            location.href = json
        }
    })
    window.scrollTo(0,50);

}

/*删除函数*/
function deleteUser(nid,i) {
    layer.confirm("您确定要删除?",  {icon: 3, title:'提示'}, function(e) {
        $.post("notice/deleteByNid",{nid:nid},function (json) {
            if (json){
                $.ready(page(i,10))
            }
        })
        layer.close(e)
    });
}
/*更新查询，并放入模态框*/
function updateUser(id,i) {
    $("#modal2").html("<div class=\"modal fade\" id=\"updateModal\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\">\n" +
        "    <div class=\"modal-dialog\" role=\"document\">\n" +
        "        <div class=\"modal-content\">\n" +
        "            <div class=\"modal-header\">\n" +
        "                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n" +
        "                <h4 class=\"modal-title\" id=\"myModalLabel\">修改公告信息</h4>\n" +
        "            </div>\n" +
        "            <div class=\"modal-body\">\n" +
        "                <form id=\"updateFrom\" class=\"form-horizontal\">\n" +
        "                    <input id=\"nid\" hidden=\"hidden\" type=\"text\" name=\"nid\">\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"theme\" class=\"col-sm-2 control-label\">主题：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <input type=\"text\" name=\"theme\" class=\"form-control\" id=\"theme\" placeholder=\"主题\">\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"content\" class=\"col-sm-2 col-xs-2 control-label\">内容</label>\n" +
        "                        <div class=\"col-sm-10 col-xs-5\">\n" +
        "                             <textarea id=\"content\" name=\"content\" class=\"form-control\" rows=\"4\"></textarea>\n" +
        "                          </div>\n" +
        "                       </div>\n" +
        "                </form>\n" +
        "            </div>\n" +
        "            <div class=\"modal-footer\">\n" +
        "                <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>\n" +
        "                <button type=\"button\" id=\"updateSubmit\" class=\"btn btn-primary\">Save</button>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>\n" +
        "</div>");
    $.post("notice/findByNid",{nid:id},function (json) {
        if (json!="index.jsp"){
            var notice = json
            $("#nid").attr({value:id});
            $("#theme").attr({value:notice.theme});
            $("#content").html(notice.content);
        }else {
            location.href = ''+json+''
        }
    })

    $("#updateModal").modal({
        backdrop:"static"
    })

    $("#updateSubmit").click(function () {
        layer.confirm("您确定要修改?",  {icon: 3, title:'提示'}, function(e) {
            $.post("notice/updateNotice", $("#updateFrom").serialize(), function (data) {
            })
            $("#updateModal").modal('hide')
            $.ready(page( i, 10))
            layer.close(e)
        })
    })
}

